package com.example.minzhengtong.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.minzhengtong.R
import com.example.minzhengtong.base.BaseFg
import com.example.minzhengtong.data.BannerBean
import com.example.minzhengtong.data.ChildBean
import com.example.minzhengtong.databinding.FragmentTChildBinding
import com.example.minzhengtong.ui.adapter.AdapterBanner
import com.example.minzhengtong.ui.adapter.AdapterThirdBanner
import com.youth.banner.listener.OnPageChangeListener
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder

/**
 * created by WY on 2021/6/2.
 */
class TChildFG : BaseFg() {

    companion object {
        @JvmStatic
        fun newInstance(param1: String = "", param2: String = "") =
            TChildFG().apply {
                arguments = Bundle().apply {
                }
            }
    }

    private lateinit var binding : FragmentTChildBinding

    private val mData = mutableListOf<ChildBean>()
    private val mAdapter by lazy {
        ChildAdapter(mData)
    }
    private val mData2 = mutableListOf<ChildBean>()
    private val m2Adapter by lazy {
        ChildAdapter(mData2)
    }

    private val mBanner = mutableListOf<BannerBean>()
    private val mBannerMiddle = mutableListOf<BannerBean>()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentTChildBinding.inflate(inflater)
        return binding.root
    }

    override fun initView() {

        mData.add(ChildBean(logo = R.mipmap.tup1,title = "文化旅游谱写新时代山海情主旋律",time = "福建省人民政府门户网站    2021-05-28"))
        mData.add(ChildBean(logo = R.mipmap.tup2,title = "习近平致信祝贺中国日报创刊40周年",time = "福建省人民政府门户网站    2021-05-28"))
        mData.add(ChildBean(logo = R.mipmap.tup3,title = "省十三届人大常委会第二十七次会议闭幕",time = "福建省人民政府门户网站    2021-05-28"))
        mData.add(ChildBean(logo = R.mipmap.tup4,title = "加强和改进国际传播工作展示真实立体全面的中国",time = "福建省人民政府门户网站    2021-06-02"))

        mData2.add(ChildBean(logo = R.mipmap.tup10,title = "福建省新型冠状病毒肺炎疫情情况",time = "福建省人民政府门户网站    2021-06-02"))
        mData2.add(ChildBean(logo = R.mipmap.tup6,title = "文化旅游谱写新时代山海情主旋律",time = "福建省人民政府门户网站    2021-05-28"))
        mData2.add(ChildBean(logo = R.mipmap.tup8,title = "文化旅游谱写新时代山海情主旋律",time = "福建省人民政府门户网站    2021-05-28"))

        mBanner.add(BannerBean(logo = R.mipmap.t_child_banner1,title = "坚持自立自强、创新引导高标准高质量推进数字中……"))
        mBanner.add(BannerBean(logo = R.mipmap.t_child_banner2,title = "习近平在福州考察调研"))
        mBanner.add(BannerBean(logo = R.mipmap.t_child_banner3,title = "习近平赴福建考察调研"))

        mBannerMiddle.add(BannerBean(logo = R.mipmap.banner1,title = ""))
        mBannerMiddle.add(BannerBean(logo = R.mipmap.banner2,title = ""))
        mBannerMiddle.add(BannerBean(logo = R.mipmap.banner3,title = ""))
        mBannerMiddle.add(BannerBean(logo = R.mipmap.banner4,title = ""))

        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(baseContext,LinearLayoutManager.VERTICAL,false)
            adapter = mAdapter
        }

        binding.recyclerView2.apply {
            layoutManager = LinearLayoutManager(baseContext,LinearLayoutManager.VERTICAL,false)
            adapter = m2Adapter
        }

        val inflater = LayoutInflater.from(baseContext)

        binding.banner.apply {
            adapter = AdapterThirdBanner(mBanner)
            for (i in mBanner.indices){
                val normalDot = inflater.inflate(R.layout.third_dot,binding.indicatorContainer,false) as ImageView
                normalDot.setImageResource(if (i == currentItem) R.drawable.dot_select else R.drawable.dot_normal)
                binding.indicatorContainer.addView(normalDot)
            }
            addOnPageChangeListener(object : OnPageChangeListener{
                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
                ) {

                }

                override fun onPageSelected(position: Int) {
                    binding.title.text = mBanner[position].title
                    for (i in mBanner.indices){
                        val initDot = binding.indicatorContainer.getChildAt(i) as ImageView
                        initDot.setImageResource(R.drawable.dot_normal)
                    }
                    val selectDot = binding.indicatorContainer.getChildAt(position) as ImageView
                    selectDot.setImageResource(R.drawable.dot_select)
                }

                override fun onPageScrollStateChanged(state: Int) {

                }
            })
        }

        binding.middleBanner.apply {
//            setDatas(mBannerMiddle)
            adapter = AdapterBanner(mBannerMiddle)
        }

    }

    override fun initListener() {

    }

    override fun lazyLoad() {

    }

    inner class ChildAdapter(data : MutableList<ChildBean>) : CommonAdapter<ChildBean>(baseContext, R.layout.item_t_child,data){
        override fun convert(holder: ViewHolder, t: ChildBean, position: Int) {
            val image = holder.getView<ImageView>(R.id.image)
            image.setImageResource(t.logo)
            val title = holder.getView<TextView>(R.id.title)
            val time = holder.getView<TextView>(R.id.time)
            title.text = t.title
            time.text = t.time
        }
    }

}